/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huibur.devicecheck.enums.TypeEnums;
import com.huibur.devicecheck.exception.TypeNameRepeatException;
import com.huibur.devicecheck.mapper.TypeMapper;
import com.huibur.devicecheck.model.param.TypeParam;
import com.huibur.devicecheck.model.po.TypePO;
import com.huibur.devicecheck.model.query.TypeQuery;
import com.huibur.devicecheck.model.vo.TypeVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.request.SessionContext;
import com.huibur.devicecheck.service.ITypeService;
import com.huibur.devicecheck.utils.ConvertPageUtil;
import com.huibur.devicecheck.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, TypePO> implements ITypeService {

     @Autowired
     TypeMapper typeMapper;

    /**
     * 新增类别
     * @param typeParam
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ObjRes<Void> insertType(TypeParam typeParam) {
        TypePO typePO = typeMapper.selectOne(new LambdaQueryWrapper<TypePO>()
                .eq(TypePO::getName, typeParam.getName())
                .ne(TypePO::getIsDeleted, TypeEnums.DELETE.getCode()));
        if (null != typePO) {
            throw new TypeNameRepeatException();
        }
        TypePO type = new TypePO();
        type.setName(typeParam.getName());
        type.setIsDeleted(TypeEnums.NORMAL.getCode());
        type.setCreateAt(DateUtils.getCurrentTimestamp());
        type.setCreateBy(SessionContext.getCurrentUserSubject().getUserId());
        typeMapper.insert(type);
        return new ObjRes<Void>().success("创建成功");
    }

    /**
     * 删除类别
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        TypePO typePO = new TypePO();
        typePO.setId(id);
        typePO.setIsDeleted(TypeEnums.DELETE.getCode());
        typeMapper.updateById(typePO);
        return true;
    }

    /**
     * 编辑类别
     * @param id
     * @param typeParam
     * @return
     */
    @Override
    public ObjRes<Void> updateType(Long id, TypeParam typeParam) {
        TypePO typePO = typeMapper.selectOne(new LambdaQueryWrapper<TypePO>()
                .ne(TypePO::getId, id)
                .eq(TypePO::getName, typeParam.getName())
                .ne(TypePO::getIsDeleted, TypeEnums.DELETE.getCode()));
        if (null != typePO) {
            throw new TypeNameRepeatException();
        }
        TypePO type = new TypePO();
        type.setName(typeParam.getName());
        type.setIsDeleted(TypeEnums.NORMAL.getCode());
        type.setUpdateAt(DateUtils.getCurrentTimestamp());
        type.setUpdateBy(SessionContext.getCurrentUserSubject().getUserId());
        typeMapper.update(type,new LambdaQueryWrapper<TypePO>().eq(TypePO::getId,id));
        return new ObjRes<Void>().success("修改成功");
    }

    /**
     * 类别列表(查询并分页)
     * @param typeQuery
     * @return
     */
    @Override
    public PageRes<TypeVO> selectListType(TypeQuery typeQuery) {
        Page<TypePO> page = new Page<>(typeQuery.getCurrent(), typeQuery.getPageSize());
        IPage<TypeVO> pages = typeMapper.selectListType(page,typeQuery);
        return ConvertPageUtil.convertPage(typeQuery.getCurrent(),page.getTotal(),pages.getRecords());
    }

    /**
     * 获取类别详情
     * @param id
     * @return
     */
    @Override
    public TypeVO selectTypeById(Long id) {
        TypePO typePO = typeMapper.selectOne(new LambdaQueryWrapper<TypePO>()
                .eq(TypePO::getId, id));
        TypeVO typeVO = new TypeVO();
        typeVO.setId(typePO.getId());
        typeVO.setName(typePO.getName());
        typeVO.setIsDeleted(TypeEnums.NORMAL.getCode());
        typeVO.setCreateAt(DateUtils.getCurrentTimestamp());
        return typeVO;
    }
}
